package de.pharmacontrol.psa.beans.managed;

import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.primefaces.model.LazyDataModel;

import de.pharmacontrol.pilot.entities.LogEntry;
import de.pharmacontrol.psa.beans.interfaces.LogEntryService;

/**
 * Session Bean implementation class LogEntryBean
 */
@ManagedBean(name = "lazyLogBean")
@SessionScoped
public class LazyLogBean {

	private LazyDataModel<LogEntry> lazyModel;

	@EJB
	LogEntryService logService;

	private LogEntry selectedLogEntry;

	public LazyDataModel<LogEntry> getLazyModel() {
		lazyModel.setRowCount(getLogEntryCount());
		return lazyModel;
	}

	public LogEntry getSelectedLogEntry() {
		return selectedLogEntry;
	}

	public void setSelectedLogEntry(LogEntry selectedLogEntry) {
		this.selectedLogEntry = selectedLogEntry;
	}

	public LazyLogBean() {

	}

	@PostConstruct
	public void init() {
		lazyModel = new LazyDataModel<LogEntry>() {

			private static final long serialVersionUID = 8910202724086345181L;

			@Override
			public List<LogEntry> load(int first, int pageSize, String sortField, boolean sortOrder, Map<String, String> filters) {
				List<LogEntry> list = logService.findPagedLogEntries(first, pageSize);
				Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.INFO, "Loading the LogEntries between {0} and {1} / data size: {2}", new Object[] { first, (first + pageSize), list.size() });
				return list;
			}
		};
		
		lazyModel.setRowCount(getLogEntryCount());
	}
	
	Integer maxCount = null;
	
	public int getLogEntryCount(){
		if (maxCount == null){
			maxCount = logService.getLogEntryCount();
		}
		return logService.getLogEntryCount();
	}

}
